package hero.mps;

public class BinarySearch {
    public static int binarySearch(int[] nums, int target) {
        int from = 0;
        int to = nums.length - 1;
        while (from <= to) {
            int mid = (to + from) >>> 1;
            int midValue = nums[mid];
            if (midValue > target) {
                to = mid - 1;
            }else if (midValue < target) {
                from = mid + 1;
            }else {
                return mid;
            }
        }
        return from;
    }
    public static int searchInsert(int[] arr, int target) {
//        int index = Arrays.binarySearch(arr, target);
//        return index > 0 ? index : -index - 1;
        return binarySearch(arr, target);
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 3, 5, 7, 9};
        System.out.println(searchInsert(nums, 2));
    }
}
